﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace EAEcoreAddin.SQLWrapperClasses
{
    /*
     *Created by Alexander Schleich
     *for EMoflon:
     *http://www.moflon.org/emoflon/
     */
    class EAConnector : EA.Connector
    {
        SQLRepository repository;
        String sqlString;

        public EAConnector(SQLRepository repository, String sqlString)
        {
            this.repository = repository;
            this.sqlString = sqlString;
        }
        
        
        #region IDualConnector Members

        public string Alias
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.ConnectorEnd ClientEnd
        {
            get 
            {
                EA.ConnectorEnd connectorEnd = new EAConnectorEnd(repository as SQLRepository, sqlString,"client");
                return connectorEnd;
            }
        }

        public int ClientID
        {
            get
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Start_Object_ID")[0]);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int Color
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string ConnectorGUID
        {
            get 
            {
                String connectorGUID = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "ea_guid")[0];
                return connectorGUID;
            }
        }

        public int ConnectorID
        {
            get 
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Connector_ID")[0]);
            }
        }

        public EA.Collection Constraints
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection CustomProperties
        {
            get { throw new NotImplementedException(); }
        }

        public int DiagramID
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Direction
        {
            get
            {
                String direction = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Direction")[0]; 
                String directionDecoded = HttpUtility.HtmlDecode(direction);
                return directionDecoded;

            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int EndPointX
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int EndPointY
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string EventFlags
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string GetLastError()
        {
            throw new NotImplementedException();
        }

        public bool IsLeaf
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsRoot
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsSpec
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string MetaType
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Name
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Name")[0];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Notes
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.ObjectType ObjectType
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Properties Properties
        {
            get { throw new NotImplementedException(); }
        }

        public int RouteStyle
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int SequenceNo
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int StartPointX
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int StartPointY
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string StateFlags
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Stereotype
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Stereotype")[0];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string StereotypeEx
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string StyleEx
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Subtype
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.ConnectorEnd SupplierEnd
        {
            get 
            {
                EA.ConnectorEnd supplierEnd = new EAConnectorEnd(repository as SQLRepository, sqlString, "target");
                return supplierEnd;
            }
        }

        public int SupplierID
        {
            get
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "End_Object_ID")[0]);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        EA.Collection taggedValues;
        public EA.Collection TaggedValues
        {
            get 
            {
                if (taggedValues != null)
                    return taggedValues;
                else
                {
                    taggedValues = new EACollection();

                    String[] conTags = null;
                    if (repository.FullDatabaseCheckout)
                    {
                        conTags = repository.t_connectortagElementID.GetValues(ConnectorID.ToString());
                    }
                    else
                    {
                        String contagQry = repository.SQLQuery("select * from t_connectortag where ElementID = " + ConnectorID);
                        conTags = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(contagQry, "Row").ToArray();
                    }
                    if (conTags == null)
                        return taggedValues;

                    foreach (String actTag in conTags)
                    {
                        if (actTag != "")
                        {
                            EA.ConnectorTag newTag = new EAConnectorTag(repository, actTag);
                            (taggedValues as EACollection).AddNew(newTag);
                        }
                    }
                    return taggedValues;
                }
            }
        }

        public EA.Collection Templates
        {
            get
            {
                EA.Collection templates = new EACollection();

                String[] templatesArray = null;
                if (repository.FullDatabaseCheckout)
                {
                    templatesArray = repository.t_xrefElementGUID.GetValues(ConnectorGUID);
                }
                else
                {
                    String taggedQry = repository.SQLQuery("select * from t_xref where Client = " + "'" + ConnectorGUID + "'");
                    templatesArray = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(taggedQry, "Row").ToArray();
                }
                if (templatesArray == null)
                    return templates;
                foreach (String templateEntry in templatesArray)
                {
                    if (templateEntry != "")
                    {
                        EATemplate newTemplate = new EATemplate(repository, templateEntry);
                        (templates as EACollection).AddNew(newTemplate);
                    }
                }
                return templates;
            }
        }

        public string TransitionAction
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string TransitionEvent
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string TransitionGuard
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "PDATA2")[0];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Type
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlString, "Connector_Type")[0];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool Update()
        {
            throw new NotImplementedException();
        }

        public string VirtualInheritance
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int Width
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string get_MiscData(int index)
        {
            throw new NotImplementedException();
        }

        #endregion

        #region IDualConnector Members


        public EA.Collection ConveyedItems
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
}
